package com.baozhun.mall.common.widget.dialog

import android.content.Context
import android.text.method.LinkMovementMethod
import android.view.Gravity
import android.view.View
import com.baozhun.mall.common.R
import com.baozhun.mall.common.base.BaseApplication
import com.baozhun.mall.common.databinding.DialogCommonBinding
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.lxj.xpopup.interfaces.OnCancelListener
import com.lxj.xpopup.interfaces.OnConfirmListener

/**
 * @Title：通用dialog
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2023/5/30
 * @Version：1.0.0
 */
class CommonDialog(
    context: Context,
    var onCancelListener: OnCancelListener? = null,
    var onConfirmListener: OnConfirmListener?
) : BaseCenterPopupView<DialogCommonBinding>(context) {

    private var title: String? = ""
    private var content: CharSequence? = null
    private var contentGravity = Gravity.CENTER
    private var cancelText: String? = ""
    private var confirmText: String? = ""
    private var isClickableSpan: Boolean? = false
    private var mShowCancelBt: Boolean? = true

    var clickDismiss = true

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_common
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth() - SizeUtils.dp2px(34f * 2)
    }

    override fun onCreate() {
        super.onCreate()
        mBinding.tvTitle.text = title
        mBinding.tvContent.text = content
        mBinding.tvContent.gravity = contentGravity
        mBinding.tvCancel.text = cancelText
        mBinding.tvConfirm.text = confirmText
        if (isClickableSpan == true) {
            mBinding.tvContent.movementMethod = LinkMovementMethod.getInstance()
            mBinding.tvContent.highlightColor = context.getColor(android.R.color.transparent) //方法重新设置文字背景为透明色。
        }
        mBinding.showCancelBt = mShowCancelBt
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.tv_cancel -> {
                onCancelListener?.onCancel()
                dismiss()
            }
            R.id.tv_confirm -> {
                if (clickDismiss) dismiss()
                onConfirmListener?.onConfirm()
            }
        }
    }

    fun title(titleReId: Int): CommonDialog {
        this.title = BaseApplication.mInstance.getString(titleReId)
        return this
    }

    fun title(title: String): CommonDialog {
        this.title = title
        return this
    }

    /**
     * 点击事件操作后dialog是否消失
     */
    fun clickNotDismiss(): CommonDialog {
        clickDismiss = false
        return this
    }

    fun content(contentResId: Int): CommonDialog {
        this.content = BaseApplication.mInstance.getString(contentResId)
        return this
    }

    fun contentGravity(gravity: Int): CommonDialog {
        this.contentGravity = gravity
        return this
    }

    fun showCancelBt(showCancelBt: Boolean): CommonDialog {
        this.mShowCancelBt = showCancelBt
        return this
    }

    fun isClickableSpan(isClickableSpan: Boolean): CommonDialog {
        this.isClickableSpan = isClickableSpan
        return this
    }

    fun content(contentCharSequence: CharSequence): CommonDialog {
        this.content = contentCharSequence
        return this
    }

    fun cancelText(cancelTextResId: Int): CommonDialog {
        this.cancelText = BaseApplication.mInstance.getString(cancelTextResId)
        return this
    }

    fun confirmText(confirmTextResId: Int): CommonDialog {
        this.confirmText = BaseApplication.mInstance.getString(confirmTextResId)
        return this
    }

    fun cancelText(cancelText: String): CommonDialog {
        this.cancelText = cancelText
        return this
    }

    fun confirmText(confirmText: String): CommonDialog {
        this.confirmText = confirmText
        return this
    }
}